import * as server from "@minecraft/server";
import * as data_entity from "../data/entity";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
export function ChorusPictureTrigger() {
    return {
        onUse(source) {
            const player = source.source;
            const item = source.itemStack;
            const container = player.getComponent('inventory')?.container;
            if (!container || !player || !item)
                return;
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 140))
                return;
            const testHeight = player.location.y < player.dimension.heightRange.min + 32;
            const rule = player.getDynamicProperty('entity:house_create_after') ?? false;
            const analysis = derived_tools.AnalysisWeight(data_entity.chorus_call_role);
            if (testHeight)
                return player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 当前高度不足, 无法使用<§l§9 百灵绘卷 §r>' }]);
            derived_tools.CompleteSummonAnimation(player, player.getHeadLocation());
            container?.setItem(player.selectedSlotIndex);
            if (!rule)
                return InitialSummoning(player);
            server.system.runTimeout(() => RandomCallRole(player, analysis.source, analysis.index), 40);
            native_tools.IntelMessage(player, 5, '新的伙伴已经出现, 快使用§9[§n§l 精灵结契 §r§9]§r与§9[§6 领航者 §9]§r缔结属于你们的§9[§u 契约 §9]§r吧!!');
        }
    };
}
;
function InitialSummoning(player) {
    const queue = ['entity_adrift:crystal', 'entity_adrift:moonlight', 'entity_adrift:roaring'];
    server.system.runTimeout(() => AppointCallRole(player, queue), 8);
    player.setDynamicProperty('entity:house_create_after', true);
    derived_tools.PlayPrompt(player, "召唤星辉雅居");
    StarlightHouse(player);
}
;
function StarlightHouse(player) {
    const rule = server.world.getDynamicProperty('game_rules:regenerate.starlight_house') ?? true;
    const reference = math_tools.Vector.add(player.location, { x: -13, y: -15, z: -13 });
    const template = server.world.structureManager.get('mystructure:starlight_house');
    if (rule === false)
        return EmergencyPlan(player);
    if (!template)
        return player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 未能获取到<§l§9 星辉雅居 §r>的结构数据文件' }]);
    native_tools.TryFillBlocks(player.dimension, reference, math_tools.Vector.add(reference, { x: 24, y: 24, z: 24 }), 'minecraft:white_stained_glass');
    server.system.runTimeout(() => server.world.structureManager.place(template, player.dimension, reference), 2);
    server.system.runTimeout(() => player.applyKnockback((Math.random() * 2) - 1, (Math.random() * 2), 1, -1), 4);
    if (rule == true)
        server.world.setDynamicProperty('game_rules:regenerate.starlight_house', false);
}
;
function EmergencyPlan(player) {
    const subsidy = [
        new server.ItemStack('item_tool:magic_crystal_screwdriver'),
        new server.ItemStack('item_tool:magic_crystal_shield'),
        new server.ItemStack('item_tool:magic_crystal_hammer'),
        new server.ItemStack('item_tool:magic_crystal_wrench'),
        new server.ItemStack('item_tool:magic_crystal_key'),
        new server.ItemStack('item_tool:magic_crystal_bow'),
        new server.ItemStack('item_tool:magic_crystal_hoe'),
        new server.ItemStack('item_literature:magic_handbook'),
        new server.ItemStack('item_literature:faerie_healing'),
        new server.ItemStack('item_equipment:magic_crystal_armor_lv1'),
        new server.ItemStack('minecraft:barrel')
    ];
    subsidy.forEach(item => native_tools.TrySpawnItem(player.dimension, item, player.location));
}
;
function RandomCallRole(player, queue, index) {
    const vertex0 = math_tools.Vector.add(player.location, { x: 3, y: 5, z: 3 });
    const vertex1 = math_tools.Vector.add(player.location, { x: -3, y: 0, z: -3 });
    const anchor = math_tools.Vector.random(vertex0, vertex1);
    player.spawnParticle('constant:the_cracks_of_the_misty_sea', anchor);
    if (!player.getDynamicProperty('entity:healer_role_up')) {
        const choice = derived_tools.AnalysisWeight(data_entity.call_healer_role);
        player.setDynamicProperty('entity:healer_role_up', true);
        native_tools.TrySpawnEntity(player.dimension, choice.output, anchor);
        return;
    }
    ;
    if (!player.getDynamicProperty('entity:fortify_role_up')) {
        const choice = derived_tools.AnalysisWeight(data_entity.call_fortify_role);
        player.setDynamicProperty('entity:fortify_role_up', true);
        native_tools.TrySpawnEntity(player.dimension, choice.output, anchor);
        return;
    }
    ;
    switch (math_tools.Random({ min: 1, max: 32 }, true)) {
        case 16:
            WaspClusterRaidTrigger.Create(player.id, 20, { location_group: [player.getHeadLocation()], dimension_group: [player.dimension] });
            player.onScreenDisplay.setTitle('§c§l< 野蜂 袭击 > !!!§r');
            native_tools.TrySpawnEntity(player.dimension, queue[index], anchor);
            break;
        default:
            native_tools.TrySpawnEntity(player.dimension, queue[index], anchor);
            break;
    }
}
;
function AppointCallRole(player, queue) {
    const vertex0 = math_tools.Vector.add(player.location, { x: 2, y: 2, z: 2 });
    const vertex1 = math_tools.Vector.add(player.location, { x: -2, y: 0, z: -2 });
    player.spawnParticle('constant:the_cracks_of_the_misty_sea', math_tools.Vector.random(vertex0, vertex1));
    queue.forEach(entity => native_tools.TrySpawnEntity(player.dimension, entity, math_tools.Vector.random(vertex0, vertex1)));
}
;
class WaspClusterRaidTrigger extends achieve_plan.Template {
    eventTimer = 0;
    afterPlanEvent(data) {
        if (!this.annex.dimension_group)
            return data.remove();
        if (!this.annex.location_group)
            return data.remove();
        const dimension = this.annex.dimension_group[0];
        const proto = this.annex.location_group[0];
        const current = math_tools.Vector.add(proto, { x: Math.random() * 16 - 8, y: Math.random() * 16, z: Math.random() * 16 - 8 });
        native_tools.TrySpawnParticle(dimension, 'constant:the_cracks_of_the_misty_sea', current);
        const analysis = derived_tools.AnalysisWeight(data_entity.wasp_cluster_raid);
        const wasp = native_tools.TrySpawnEntity(dimension, analysis.output, current);
        if (wasp instanceof server.Entity)
            wasp.nameTag = '§u野蜂 袭击§r';
        if (this.eventTimer >= 15) {
            native_tools.TrySpawnItem(dimension, new server.ItemStack('item_crystal:eternal_energy'), current);
            native_tools.TrySpawnParticle(dimension, 'constant:fireworks_salvo_rune_red', current);
            native_tools.TrySpawnParticle(dimension, 'constant:disperse_rune_red', current);
            native_tools.TrySpawnParticle(dimension, 'constant:erupt_rune_red', current);
            return data.remove();
        }
        ;
        this.eventTimer += 1;
    }
    ;
}
;
export function FlowingStarTrigger() {
    return {
        onUse(source) {
            const player = source.source;
            const item = source.itemStack;
            const container = player.getComponent('inventory')?.container;
            if (!container || !player || !item)
                return;
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 140))
                return;
            const testHeight = player.location.y < player.dimension.heightRange.min + 32;
            if (testHeight)
                return player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 当前高度不足, 无法使用<§l§9 流光之星 §r>' }]);
            container?.setItem(player.selectedSlotIndex);
            const reference = math_tools.Vector.add(player.location, { x: -13, y: -15, z: -13 });
            const template = server.world.structureManager.get('mystructure:starlight_house');
            native_tools.TryFillBlocks(player.dimension, reference, math_tools.Vector.add(reference, { x: 24, y: 24, z: 24 }), 'minecraft:white_stained_glass');
            if (!template)
                return player.sendMessage([native_tools.EntityTranslate(player), { text: '-> 未能获取到<§l§9 星辉雅居 §r>的结构数据文件' }]);
            server.system.runTimeout(() => server.world.structureManager.place(template, player.dimension, reference), 2);
            server.system.runTimeout(() => player.applyKnockback((Math.random() * 2) - 1, (Math.random() * 2), 1, -1), 4);
            native_tools.IntelMessage(player, 5, '『 星辉雅居 』');
            derived_tools.CompleteSummonAnimation(player, player.getHeadLocation());
        }
    };
}
;
export function MoonAndStarsTrigger() {
    return {
        onUse(source) {
            const player = source.source;
            const item = source.itemStack;
            const container = player.getComponent('inventory')?.container;
            if (!container || !player || !item)
                return;
            if (!achieve_plan.ControlEventTrigger.EntityToken(item.typeId, player, 80))
                return;
            const typeId = item.getDynamicProperty('reduction_pureness:type');
            if (!typeId)
                return player.sendMessage(`§l§4<§c 物品数据 §4>§r数据丢失, 无法继续使用`);
            item.getComponent('minecraft:cooldown')?.startCooldown(player);
            native_tools.SummoningEntityWithData(player, container, typeId);
            derived_tools.ParticleSummonAnimation(player, player.location);
        }
    };
}
;
